package com.wandian.cqcsk.mvvm.videoylspfragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.eventbus.ChangeMessage;
import com.mylib.libcore.mvvm.BaseMVVMFragment;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.cqcsk.R;
import com.wandian.cqcsk.databinding.FragmentHomeYlspBinding;
import com.yilan.sdk.data.user.YLUser;
import com.yilan.sdk.ui.configs.CommentConfig;
import com.yilan.sdk.ui.configs.YLUIConfig;
import com.yilan.sdk.ui.little.YLLittleVideoFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 一览视频
 **/
@Route(path = RouterConfig.ROUTER_FRAGMENT_VIDEO_YLSP)
public class VideoYLSPFragment extends BaseMVVMFragment<VideoYLSPFragmentViewModel> {
    private FragmentHomeYlspBinding dataBinding;
    private FragmentManager mSupportFragmentManager;
    private YLLittleVideoFragment littleVideoFragment;
    private UserInfoBean userInfoBean;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        try {
            EventBus.getDefault().unregister(this);
        }catch (Exception e){}
        super.onDestroyView();
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home_ylsp, container, false);
        return dataBinding.getRoot();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        dataBinding.setViewModel(mViewModel);
        if(!UIUtils.isEmpty(dataBinding))EventBus.getDefault().register(this);
        initClink();
        Log.d("ylsp","--->start");
    }



    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d("ylsp","--->isVisibleToUser"+isVisibleToUser);
        if(littleVideoFragment!= null)
            littleVideoFragment.setUserVisibleHint(!isVisibleToUser);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("ylsp","--->onResume");
//        if(littleVideoFragment!= null) littleVideoFragment.onResume();
        if(mSupportFragmentManager!= null && littleVideoFragment!= null)  mSupportFragmentManager.beginTransaction().show(littleVideoFragment).commit();

    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("ylsp","--->onPause");
        if(littleVideoFragment!= null) littleVideoFragment.onPause();
        if(mSupportFragmentManager!= null && littleVideoFragment!= null)  mSupportFragmentManager.beginTransaction().hide(littleVideoFragment).commit();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.d("ylsp","--->hidden"+hidden);
        if(littleVideoFragment!= null) littleVideoFragment.onHiddenChanged(hidden);
    }

    private void initViewData(){
        if(littleVideoFragment==null){
            //设置用户信息
            userInfoBean = AppContext.getInstance().getAppPreferencesHelper().getUserInfoBean();
            YLUser.getInstance().login(userInfoBean.getNickName()+"", userInfoBean.getHeadPic()+"", "13666666666", userInfoBean.getId()+"");
            // 页面UI配置
            YLUIConfig.getInstance()
                    .littleLikeShow(true)//是否显示小视频点赞
                    .littleShareShow(true)//是否显示小视频分享
                    .littleShowGuide(true)//是否显示新手引导滑动浮层（第一次安装进入显示）
                    .littleComment(CommentConfig.CommentType.SHOW_COMMENT_ALL)//小视频评论展现形式
                    .videoComment(CommentConfig.CommentType.SHOW_COMMENT_ALL)//短视频评论展现形式
                    .videoLikeShow(true)//是否显示短视频点赞
                    .videoShareShow(true)//是否显示短视频分享
                    .followAvailable(true)//是否显示关注按钮
                    .followChannelAvailable(true)//是否在频道页面展示关注
                    .feedAvatarClickable(true)//短视频头像是否可以点击
                    .setLittleTitleBottom(0)//设置小视频标题距离底部的间距，单位dp
                    .feedPlayAuto(true);//feed自动播放（仅在FeedConfig.getInstance().setPlayerStyle(FeedConfig.STYLE_FEED_PLAY) 时有效）
            mSupportFragmentManager = getActivity().getSupportFragmentManager();
            littleVideoFragment = YLLittleVideoFragment .newInstance();
            mSupportFragmentManager.beginTransaction().replace(R.id.my_yl_fragment, littleVideoFragment).commitAllowingStateLoss();

        }
    }

    private void initClink(){
    //    dataBinding.ivHot.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_VIDEO_HOT_SORT).withString("video_type","hot").navigation());
    //    dataBinding.ivSort.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_VIDEO_HOT_SORT).withString("video_type","sort").navigation());
      //  dataBinding.ivList.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_VIDEO_HOT_SORT).withString("video_type","list").navigation());
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBusMessage(ChangeMessage changeMessage) {
        try {
            if(!UIUtils.isEmpty(changeMessage) && !UIUtils.isEmpty(changeMessage.getIsChange())){
                if(changeMessage.getIsChange().equals("changeVideo")){
                    Log.d("changeVideo","changeVideo" );
                    initViewData();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
